package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.controller.convert.AuthRolePermissionDTOConvert;
import com.jingdianjichi.application.controller.dto.AuthRolePermissionDTO;
import com.jingdianjichi.auth.domain.bo.AuthRolePermissionBO;
import com.jingdianjichi.auth.domain.service.AuthRolePermissionDomainService;
import com.jingdianjichi.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ClassName:AuthRolePermissionController
 * Package:com.jingdianjichi.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/10/10 17:24
 * @Version 1.0
 */
@RequestMapping("/rolePermission/")
@Slf4j
@RestController
public class AuthRolePermissionController
{
        @Resource
        private AuthRolePermissionDomainService authRolePermissionDomainService;

        @PostMapping("add")
        public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO)
        {
            try {
                if (log.isInfoEnabled()) {
                    log.info("AuthRolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
                }
                Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),"RoleId不能为空");
                Preconditions.checkNotNull(authRolePermissionDTO.getPermissionIdList(),"permissionIdlist不能为空");

                AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConvert.INSTANDE.convertDTOTOBORolePermission(authRolePermissionDTO);

                authRolePermissionDomainService.add(authRolePermissionBO);

                return Result.ok(true);
            } catch (Exception e) {
                log.error("AuthRolePermissionController .add.error:{}", e.getMessage(), e);
                return Result.fail(e.getMessage());
            }
        }
}
